package com.atguigu.lease.web.admin.controller.apartment;


import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.entity.AttrKey;
import com.atguigu.lease.model.entity.AttrValue;
import com.atguigu.lease.web.admin.service.AttrKeyService;
import com.atguigu.lease.web.admin.service.AttrValueService;
import com.atguigu.lease.web.admin.vo.attr.AttrKeyVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Tag(name = "房间属性管理")
@RestController
@RequestMapping("/admin/attr")
public class AttrController {

    @Autowired
    private AttrKeyService attrKeyService;

    @Autowired
    private AttrValueService attrValueService;

    @Operation(summary = "新增或更新属性名称")
    @PostMapping("/key/saveOrUpdate")
    public Result saveOrUpdateAttrKey(@RequestBody AttrKey attrKey) {
        boolean saveOrUpdate = attrKeyService.saveOrUpdate(attrKey);
        return saveOrUpdate ? Result.ok() : Result.fail();
    }

    @Operation(summary = "新增或更新属性值")
    @PostMapping("/value/saveOrUpdate")
    public Result saveOrUpdateAttrValue(@RequestBody AttrValue attrValue) {
        boolean saveOrUpdate = attrValueService.saveOrUpdate(attrValue);
        return saveOrUpdate ? Result.ok() : Result.fail();
    }

    /**
     * 删除主表数据 attr_key，也应该删除对应子表数据 attr_value 否则会产生孤儿数据（垃圾数据）
     *
     * @param attrKeyId
     * @return
     */
    @Operation(summary = "根据id删除属性名称")
    @DeleteMapping("/key/deleteById")
    public Result removeAttrKeyById(@RequestParam Long attrKeyId) {
        // 根据主键删除 key
        boolean remove = attrKeyService.removeById(attrKeyId);
        if (remove) {
            // 删除 对应的 attr_value 表中数据
            LambdaQueryWrapper<AttrValue> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(AttrValue::getAttrKeyId, attrKeyId);
            attrValueService.remove(queryWrapper);
            return Result.ok();
        }
        return Result.fail();
    }

    @Operation(summary = "根据id删除属性值")
    @DeleteMapping("/value/deleteById")
    public Result removeAttrValueById(@RequestParam Long id) {
        boolean remove = attrValueService.removeById(id);
        return remove ? Result.ok() : Result.fail();
    }


    /**
     * 查询公寓属性和属性值
     * 实现：
     * 1. 改造 VO
     * 2. 定义 service
     * 3. 改造 mapper
     * 4. 定义 mapper.xml 多表查询 + resultMap 映射
     * 问题：
     * 1. 返回结果问题 -》 单个实体类无法完成 key value
     * 创建一个 VO -> 本次接收或者返回的结果
     * public class AttrKeyVo extends AttrKey {
     *
     * @return
     * @Schema(description = "属性value列表")
     * private List<AttrValue> attrValueList;
     * }
     * 2. 本次是多表查询，attr_key 和 attr_value 自定义查询条件
     * 3. 自定义 sql 查询，需要关注逻辑删除字段
     */
    @Operation(summary = "查询全部属性名称和属性值列表")
    @GetMapping("/list")
    public Result<List<AttrKeyVo>> listAttrInfo() {
        List<AttrKeyVo> list = attrKeyService.customList();
        return Result.ok(list);
    }
}
